package com.woniu.day8.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.woniu.day8.pojo.BuildingPo;
import com.woniu.day8.pojo.dto.BuildingDto;
import com.woniu.day8.service.BuildingServicer;
import com.woniu.day8.util.ResultMessage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/building")
@SaCheckLogin
public class BuildingController {
    @Autowired
    private BuildingServicer buildingService;
    /**
     * 添加楼道
     */
    @PostMapping("/insertBuild")
    public ResultMessage insertBuild(@RequestBody BuildingDto Bdto){
       //如果数据库中有数据则返回失败
        if (buildingService.exists(new QueryWrapper<BuildingPo>().eq("remarks",Bdto.getRemarks()))){
            return ResultMessage.fail("添加失败,此楼号已存在");
        }
        BuildingPo Bpo = new BuildingPo();
        BeanUtils.copyProperties(Bdto,Bpo);
        buildingService.save(Bpo);
        return ResultMessage.success("添加成功");
    }
    /**
     * 删除楼道
     */
    @DeleteMapping("/deleteBuild")
    public ResultMessage deleteBuild(@ModelAttribute BuildingDto Bdto){
        if (!buildingService.exists(new QueryWrapper<BuildingPo>().eq("id",Bdto.getId()))){
            return ResultMessage.fail("删除失败,此楼号不存在");
        }
        BuildingPo Bpo = new BuildingPo();
        BeanUtils.copyProperties(Bdto,Bpo);
        buildingService.removeById(Bpo.getId());
        return ResultMessage.success("删除成功");
    }
    /**
     * 修改楼道
     */
    @PutMapping("/putBuild")
    public ResultMessage putBuild(@RequestBody BuildingDto Bdto){
        if (!buildingService.exists(new QueryWrapper<BuildingPo>().eq("id",Bdto.getId()))){
            return ResultMessage.fail("修改失败,此楼号不存在");
        }
        BuildingPo Bpo = new BuildingPo();
        BeanUtils.copyProperties(Bdto,Bpo);
        buildingService.updateById(Bpo);
        return ResultMessage.success("修改成功");
    }
    /**
     * 查询楼道(分页+多条件查询)
     */

    @GetMapping("/selectBuild")
    public ResultMessage selectBuild(@ModelAttribute BuildingDto Bdto){
        Long pageNum = Bdto.getPageNum();//拿到页码
        Long pageSize = Bdto.getPageSize();//拿到页大小
        Page<BuildingPo> page = new Page<>(pageNum, pageSize);
        BuildingPo Bpo = new BuildingPo();
        BeanUtils.copyProperties(Bdto,Bpo);
        Page<BuildingPo> buildingPoPage = buildingService.buildPageInfo(page, Bpo);
        Map<String, Object> result = new HashMap<>();
        result.put("records", buildingPoPage.getRecords());//查询数据
        result.put("total", buildingPoPage.getTotal());//数据总跳数
        result.put("current", buildingPoPage.getCurrent());//当前页
        result.put("pages", buildingPoPage.getPages());//总页数
        result.put("size", buildingPoPage.getSize());//页大小

        return ResultMessage.success(result);
    }
}
